
#ifndef _WEBACTIVITE_H_
#define _WEBACTIVITE_H_

#include "../core/webpageadapter.h"
#include "webspideraround.h"

#include "../../third/log4qt/src/log4qt/logger.h"
#include "../../third/log4qt/src/log4qt/basicconfigurator.h"
#include "../../third/log4qt/src/log4qt/propertyconfigurator.h"

class WebActivite : public PageProtocol {
    public:
        WebActivite(WebSpiderAround *spiderAround);
        ~WebActivite();

        Operator* startWithCompletedWebPage(WebPage *page);
        void start();
        void stop();

        QString url();
        long ref();
        std::string clientID();

        int loadingProcess();

        void didLoadStart(Operator *opt);
        void didInitialized(Operator *opt);
        void didLoadStarted(Operator *opt);

        void didSelfDestroyParse(Operator* opt, QString & status);
        void didTimeout(Operator* opt, QString & status);
        void didLoadFinished(Operator *opt, const QString & status);

        void didJavaScriptAlertSent(Operator *opt, const QString & msg);
        void didJavaScriptConsoleMessageSent(Operator *opt, const QString & message);
        void didJavaScriptErrorSent(Operator *opt, const QString & msg, const QString & stack);

        void didResourceRequested(Operator *opt, const QVariant & requestData, QObject * request);
        void didResourceReceived(Operator *opt, const QVariant & resource);
        void didResourceError(Operator *opt, const QVariant & errorData);
        void didResourceTimeout(Operator *opt, const QVariant & errorData);

        void didUrlChanged(Operator *opt, const QUrl & url);

        void didNavigationRequested(Operator *opt, const QUrl & url, const QString & navigationType, bool navigationLocked, bool isMainFrame);
        void didRawPageCreated(Operator *opt, QObject * page);
        void didClosing(Operator *opt, QObject * page);

        WebPageAdapter *webAdapter();

        int level();

        long requestTs() { return m_requestTs; }
        long startTs() { return m_startTs; }

    private:
        void destoryWebPageAdapter();
        void fireRequest();

        WebPageAdapter *m_adapter;
        WebSpiderAround *m_spiderAround;
        QString m_url;
        // completely finished url
        QString m_completelyUrl;
        QMap<QString, QVariantList> m_urlHeadersMap;

        int m_loadFaildCount;
        int m_loadCanceledCount;
        int m_loadingProcess;
        Log4Qt::Logger *m_logger;
        int m_hasLoadResource;
        long m_requestTs;
        long m_startTs;
};

#endif

